package xmlToJava;

import java.util.ArrayList;

import modelo.VirtualMachineModel;

import com.thoughtworks.xstream.XStream;

import elementosDoModelo.Context;
import elementosDoModelo.Disk;
import elementosDoModelo.Graphics;
import elementosDoModelo.History;
import elementosDoModelo.Nic;
import elementosDoModelo.Os;
import elementosDoModelo.Raw;
import elementosDoModelo.Template;

public class XmlUtil {

	public static ArrayList<VirtualMachineModel> xmlToJavaVMP(String s) {

		XStream xstream = new XStream();

		// mapeamento classes
		xstream.alias("VM_POOL", ArrayList.class);
		xstream.alias("VM", VirtualMachineModel.class);
		xstream.alias("TEMPLATE", Template.class);
//		xstream.alias("DISK", Disk.class);
		xstream.addImplicitCollection(Template.class, "disks","DISK",Disk.class);
		xstream.alias("GRAPHICS", Graphics.class);
		xstream.alias("OS", Os.class);
		xstream.alias("HISTORY", History.class);
		xstream.alias("CONTEXT", Context.class);
		xstream.alias("NIC", Nic.class);
		xstream.addImplicitCollection(Template.class, "raws","RAW",Raw.class);
//		xstream.alias("RAW", Raw.class);
		// mapeamento da classe  VirtualMachineModel
		xstream.aliasField("ID", VirtualMachineModel.class, "id");
		xstream.aliasField("UID", VirtualMachineModel.class, "uid");
		xstream.aliasField("USERNAME", VirtualMachineModel.class, "userName");
		xstream.aliasField("NAME", VirtualMachineModel.class, "name");
		xstream.aliasField("LAST_POLL", VirtualMachineModel.class, "lastPoll");
		xstream.aliasField("STATE", VirtualMachineModel.class, "state");
		xstream.aliasField("LCM_STATE", VirtualMachineModel.class, "lcmState");
		xstream.aliasField("STIME", VirtualMachineModel.class, "stime");
		xstream.aliasField("ETIME", VirtualMachineModel.class, "etime");
		xstream.aliasField("DEPLOY_ID", VirtualMachineModel.class, "deployId");
		xstream.aliasField("MEMORY", VirtualMachineModel.class, "memory");
		xstream.aliasField("CPU", VirtualMachineModel.class, "cpu");
		xstream.aliasField("NET_TX", VirtualMachineModel.class, "netTx");
		xstream.aliasField("NET_RX", VirtualMachineModel.class, "netRx");
		xstream.aliasField("LAST_SEQ", VirtualMachineModel.class, "lastSeq");
		xstream.aliasField("TEMPLATE", VirtualMachineModel.class, "template");
		xstream.aliasField("HISTORY", VirtualMachineModel.class, "history");
		// mapeamento da classe Template
		xstream.aliasField("CONTEXT", Template.class, "context");
//		xstream.aliasField("DISK", Template.class, "disk");
		xstream.aliasField("GRAPHICS", Template.class, "graphics");
		xstream.aliasField("MEMORY", Template.class, "memory");
		xstream.aliasField("NAME", Template.class, "name");
		xstream.aliasField("NIC", Template.class, "nic");
		xstream.aliasField("OS", Template.class, "os");
//		xstream.aliasField("RAW", Template.class, "raw");
		xstream.aliasField("VMID", Template.class, "vmid");
		// mapeamento da classe disk
		xstream.aliasField("CLONE", Disk.class, "clone");
		xstream.aliasField("DISK_ID", Disk.class, "diskId");
		xstream.aliasField("SOURCE", Disk.class, "source");
		xstream.aliasField("TARGET", Disk.class, "target");
		// mapeamento classe GRAPHICS
		xstream.aliasField("LISTEN", Graphics.class, "listen");
		xstream.aliasField("PORT", Graphics.class, "port");
		xstream.aliasField("TYPE", Graphics.class, "type");
		// mapeamento classe OSBOOTERLOADER
		xstream.aliasField("BOOTLOADER", Os.class, "bootLoader");
		xstream.aliasField("KERNEL", Os.class, "kernel");
		// mapeamento classe HISTORY
		xstream.aliasField("SEQ", History.class, "seq");
		xstream.aliasField("HOSTNAME", History.class, "hostName");
		xstream.aliasField("HID", History.class, "hid");
		xstream.aliasField("STIME", History.class, "sTime");
		xstream.aliasField("ETIME", History.class, "eTime");
		xstream.aliasField("PSTIME", History.class, "psTime");
		xstream.aliasField("PETIME", History.class, "peTime");
		xstream.aliasField("RSTIME", History.class, "rsTime");
		xstream.aliasField("RETIME", History.class, "reTime");
		xstream.aliasField("ESTIME", History.class, "esTime");
		xstream.aliasField("EETIME", History.class, "eeTime");
		xstream.aliasField("REASON", History.class, "reason");
		// mapeando classe CONTEXT
		xstream.aliasField("FILES", Context.class, "files");
		xstream.aliasField("HOSTNAME", Context.class, "hostName");
		xstream.aliasField("IP_GEN", Context.class, "ipGen");
		xstream.aliasField("TARGET", Context.class, "target");
		// mapeando nic
		xstream.aliasField("BRIDGE", Nic.class, "bridge");
		xstream.aliasField("IP", Nic.class, "ip");
		xstream.aliasField("MAC", Nic.class, "mac");
		xstream.aliasField("NETWORK", Nic.class, "network");
		xstream.aliasField("NETWORK_ID", Nic.class, "networkId");
		// mapeando raw
		xstream.aliasField("DATA", Raw.class, "data");
		xstream.aliasField("TYPE", Raw.class, "type");
		
		@SuppressWarnings({ "unchecked", "rawtypes" })
		ArrayList<VirtualMachineModel> vmp = (ArrayList) xstream.fromXML(s);

		return vmp;
	}

	public static VirtualMachineModel xmlToJavaVM(String s) {

		XStream xstream = new XStream();

		// mapeamento classes
		xstream.alias("VM", VirtualMachineModel.class);
		xstream.alias("TEMPLATE", Template.class);
//		xstream.alias("DISK", Disk.class);
		xstream.addImplicitCollection(Template.class, "disks","DISK",Disk.class);
		xstream.alias("GRAPHICS", Graphics.class);
		xstream.alias("OS", Os.class);
		xstream.alias("HISTORY", History.class);
		xstream.alias("CONTEXT", Context.class);
		xstream.alias("NIC", Nic.class);
		xstream.addImplicitCollection(Template.class, "raws","RAW",Raw.class);
//		xstream.alias("RAW", Raw.class);
		// mapeamento da classe  VirtualMachineModel
		xstream.aliasField("ID", VirtualMachineModel.class, "id");
		xstream.aliasField("UID", VirtualMachineModel.class, "uid");
		xstream.aliasField("USERNAME", VirtualMachineModel.class, "userName");
		xstream.aliasField("NAME", VirtualMachineModel.class, "name");
		xstream.aliasField("LAST_POLL", VirtualMachineModel.class, "lastPoll");
		xstream.aliasField("STATE", VirtualMachineModel.class, "state");
		xstream.aliasField("LCM_STATE", VirtualMachineModel.class, "lcmState");
		xstream.aliasField("STIME", VirtualMachineModel.class, "stime");
		xstream.aliasField("ETIME", VirtualMachineModel.class, "etime");
		xstream.aliasField("DEPLOY_ID", VirtualMachineModel.class, "deployId");
		xstream.aliasField("MEMORY", VirtualMachineModel.class, "memory");
		xstream.aliasField("CPU", VirtualMachineModel.class, "cpu");
		xstream.aliasField("NET_TX", VirtualMachineModel.class, "netTx");
		xstream.aliasField("NET_RX", VirtualMachineModel.class, "netRx");
		xstream.aliasField("LAST_SEQ", VirtualMachineModel.class, "lastSeq");
		xstream.aliasField("TEMPLATE", VirtualMachineModel.class, "template");
		xstream.aliasField("HISTORY", VirtualMachineModel.class, "history");
		// mapeamento da classe Template
		xstream.aliasField("CONTEXT", Template.class, "context");
//		xstream.aliasField("DISK", Template.class, "disk");
		xstream.aliasField("GRAPHICS", Template.class, "graphics");
		xstream.aliasField("MEMORY", Template.class, "memory");
		xstream.aliasField("NAME", Template.class, "name");
		xstream.aliasField("NIC", Template.class, "nic");
		xstream.aliasField("OS", Template.class, "os");
//		xstream.aliasField("RAW", Template.class, "raw");
		xstream.aliasField("VMID", Template.class, "vmid");
		// mapeamento da classe disk
		xstream.aliasField("CLONE", Disk.class, "clone");
		xstream.aliasField("DISK_ID", Disk.class, "diskId");
		xstream.aliasField("SOURCE", Disk.class, "source");
		xstream.aliasField("TARGET", Disk.class, "target");
		// mapeamento classe GRAPHICS
		xstream.aliasField("LISTEN", Graphics.class, "listen");
		xstream.aliasField("PORT", Graphics.class, "port");
		xstream.aliasField("TYPE", Graphics.class, "type");
		// mapeamento classe OSBOOTERLOADER
		xstream.aliasField("BOOTLOADER", Os.class, "bootLoader");
		xstream.aliasField("KERNEL", Os.class, "kernel");
		// mapeamento classe HISTORY
		xstream.aliasField("SEQ", History.class, "seq");
		xstream.aliasField("HOSTNAME", History.class, "hostName");
		xstream.aliasField("HID", History.class, "hid");
		xstream.aliasField("STIME", History.class, "sTime");
		xstream.aliasField("ETIME", History.class, "eTime");
		xstream.aliasField("PSTIME", History.class, "psTime");
		xstream.aliasField("PETIME", History.class, "peTime");
		xstream.aliasField("RSTIME", History.class, "rsTime");
		xstream.aliasField("RETIME", History.class, "reTime");
		xstream.aliasField("ESTIME", History.class, "esTime");
		xstream.aliasField("EETIME", History.class, "eeTime");
		xstream.aliasField("REASON", History.class, "reason");
		// mapeando classe CONTEXT
		xstream.aliasField("FILES", Context.class, "files");
		xstream.aliasField("HOSTNAME", Context.class, "hostName");
		xstream.aliasField("IP_GEN", Context.class, "ipGen");
		xstream.aliasField("TARGET", Context.class, "target");
		// mapeando nic
		xstream.aliasField("BRIDGE", Nic.class, "bridge");
		xstream.aliasField("IP", Nic.class, "ip");
		xstream.aliasField("MAC", Nic.class, "mac");
		xstream.aliasField("NETWORK", Nic.class, "network");
		xstream.aliasField("NETWORK_ID", Nic.class, "networkId");
		// mapeando raw
		xstream.aliasField("DATA", Raw.class, "data");
		xstream.aliasField("TYPE", Raw.class, "type");
		
		VirtualMachineModel vm = (VirtualMachineModel) xstream.fromXML(s);

		return vm;
	}

	
	
	
}
